using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using Azure.Domain.Models;
using Azure.Domain.Services;
using Rhino.Mocks;
using Azure.Web.Controllers;
using Azure.Web.Models;

using Xunit;

namespace Azure.Tests.Web.Controllers
{
    public class BlogControllerFacts : ControllerFacts<BlogController>
    {
        const int Year = 2010;
        const int Month = 1;
        const int Day = 1;
        const string Slug = "test";

        [Fact]
        public void LatestWillRedirectToTheLastPublishedPost()
        {
            var post = Test.Post;
            Mock<IBlogService>().Expect(x => x.LatestPost()).Return(post);

            var result = (RedirectToRouteResult) ClassUnderTest.Latest();

            Assert.NotNull(result);
            Assert.Equal(Blog.View, result.RouteValues["action"]);
            Assert.Equal(Blog.Controller, result.RouteValues["controller"]);
            Assert.Equal(post.Year(), result.RouteValues["year"]);
            Assert.Equal(post.Month(), result.RouteValues["month"]);
            Assert.Equal(post.Day(), result.RouteValues["day"]);
            Assert.Equal(post.Slug, result.RouteValues["slug"]);
        }

        [Fact]
        public void ViewWillLoadTheMessageFromTheBlogService()
        {
            Mock<IBlogService>().Expect(x => x.LoadPost(Year, Month, Day, Slug)).Return(Test.Post);

            var result = (ViewResult) ClassUnderTest.View(Year, Month, Day, Slug);

            Assert.Equal(2010, ((PostViewModel)result.ViewData.Model).Created.Year);
        }

        [Fact]
        public void ViewWillThrowA404IfPostIsNotFound()
        {
            Assert.Throws<HttpException>(() => ClassUnderTest.View(Year, Month, Day, Slug));
        }

        [Fact]
        public void RecentWillShowLast5Posts()
        {
            var posts = new List<Post> {new Post(), new Post(), new Post(), new Post(), new Post()};
            Mock<IBlogService>().Stub(x => x.Last(5)).Return(posts);

            var result = (ViewResult) ClassUnderTest.Recent();

            Assert.Equal(5, ((RecentPostsViewModel)result.ViewData.Model).Count);
        }
    }
}